package com.manage.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.dto.VwUserRoleDetailsDTO;
import com.manage.entity.VwUserRoleDetails;
import com.manage.result.PageResult;
import com.manage.server.mapper.VwUserRoleDetailsMapper;
import com.manage.server.service.VwUserRoleDetailsService;
import com.manage.vo.VwUserRoleDetailsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author ddw
* @description 针对表【vw_user_role_details】的数据库操作Service实现
* @createDate 2025-04-13 13:37:08
*/
@Service
public class VwUserRoleDetailsServiceImpl extends ServiceImpl<VwUserRoleDetailsMapper, VwUserRoleDetails> implements VwUserRoleDetailsService {

    @Override
    public PageResult<VwUserRoleDetailsVO> findVwUserRoleDetailsByPage(VwUserRoleDetailsDTO dto) {
        // 1. 处理分页参数（严格保持默认值）
        int pageNum = dto.getPageNum() != null && dto.getPageNum() > 0 ? dto.getPageNum() : 1;
        int pageSize = dto.getPageSize() != null && dto.getPageSize() > 0 ? dto.getPageSize() : 10;

        // 2. 创建分页对象（参数顺序严格一致）
        Page<VwUserRoleDetails> page = new Page<>(pageNum, pageSize);

        // 3. 构建动态查询条件（按视图字段严格构建）
        QueryWrapper<VwUserRoleDetails> queryWrapper = new QueryWrapper<>();

        // 精确匹配条件
        if (dto.getUserRoleId() != null) {
            queryWrapper.eq("user_role_id", dto.getUserRoleId());
        }
        if (dto.getUserId() != null) {
            queryWrapper.eq("user_id", dto.getUserId());
        }

        // 模糊查询条件
        if (StringUtils.hasText(dto.getUserName())) {
            queryWrapper.like("user_name", dto.getUserName().trim());
        }
        if (StringUtils.hasText(dto.getDepartmentName())) {
            queryWrapper.like("department_name", dto.getDepartmentName().trim());
        }
        if (StringUtils.hasText(dto.getPositionTitle())) {
            queryWrapper.like("position_title", dto.getPositionTitle().trim());
        }
        if (StringUtils.hasText(dto.getParentDepartment())) {
            queryWrapper.like("parent_department", dto.getParentDepartment().trim());
        }
        if (StringUtils.hasText(dto.getParentPositionTitle())) {
            queryWrapper.like("parent_position_title", dto.getParentPositionTitle().trim());
        }

        // 状态条件
        if (StringUtils.hasText(dto.getUserStatus())) {
            queryWrapper.eq("user_status", dto.getUserStatus());
        }

        // 4. 执行分页查询（保持视图查询方式）
        Page<VwUserRoleDetails> resultPage = this.page(page, queryWrapper);

        // 5. 转换Entity到VO（严格字段映射）
        Page<VwUserRoleDetailsVO> voPage = (Page<VwUserRoleDetailsVO>) resultPage.convert(entity -> {
            VwUserRoleDetailsVO vo = new VwUserRoleDetailsVO();
            BeanUtils.copyProperties(entity, vo);
            // 处理特殊字段转换（如有需要）
            // vo.setCustomField(entity.getXXX());
            return vo;
        });

        // 6. 返回分页结果（格式严格统一）
        return PageResult.success(voPage);
    }
}




